package org.example.easybuy.controller;

import org.example.easybuy.annotation.PermissionCheck;
import org.example.easybuy.entity.User;
import org.example.easybuy.service.OrderService;
import org.example.easybuy.service.UserService;
import org.example.easybuy.util.CustomResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
@RequestMapping("/order")
@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;

    @PermissionCheck("0")
    @GetMapping("/getUserOrderPage")
    public CustomResult getPage(@RequestParam Map<String,Object> params)
    {
        User user=userService.getUserByLoginName(params.get("loginName").toString());
        params.put("userId",user.getId());
        return CustomResult.successResult().addResult("pageInfo",orderService.getOrderPageInfo(params));
    }

    @PermissionCheck("1")
    @GetMapping("/getAdminOrderPage")
    public CustomResult getAdminOrderPage(@RequestParam Map<String,Object> params)
    {
        return CustomResult.successResult().addResult("pageInfo",orderService.getOrderPageInfo(params));
    }

    @GetMapping("/updateOrderPayment")
    public CustomResult updateOrderPayment(int id,String opera)
    {
        Map<String,Object> map=new HashMap<>();
        if(opera.equals("del"))
        {
            map.put("isDel",1);
        }
        if(opera.equals("cancel"))
        {
            map.put("isPay",2);
        }
        map.put("id",id);
        if(orderService.updateOrderStatus(map))
        {
            return CustomResult.successResult().addResult("status","success");
        }
        return CustomResult.errorResult(500,"修改状态出错了").addResult("status","fail");
    }

}
